package 
{
	import flash.display.Loader;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.display.StageScaleMode;
	import com.dd.screencapture.ScreenCapture;
	import com.dd.screencapture.SimpleFlvWriter;
	
	/**
	 * ...
	 * @author hp
	 */
	public class Main extends Sprite 
	{
		public var request:URLRequest = new URLRequest("KIO_1_ru.swf");
		public var loader:Loader = new Loader();
		private var points:Array = new Array();
		private var point:Point;
		private var screenCapture:ScreenCapture;
		private var saveFile:FileReference = new FileReference();
		private var loadFile:FileReference = new FileReference();
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			loadFile.browse();
			loadFile.addEventListener(Event.SELECT, onFileSelected);
			
			/*
			loader.load(request);
			stage.addChild(loader);
			*/
			
			loader.addEventListener(MouseEvent.CLICK, onMouseClick);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			
			try{
				screenCapture = ScreenCapture.getInstance();
				screenCapture.source = loader;
				screenCapture.fps = 2;
				screenCapture.size(stage.stageWidth, stage.stageHeight);
				screenCapture.useHandCursor = true;
				
				screenCapture.x = 10;
				screenCapture.y = 10;
			
				addChild(screenCapture);
			}
			catch (err:Error)
			{
				trace(err.getStackTrace());
			}
			
		}
		
		private function onFileSelected(e:Event):void 
		{
			loadFile.addEventListener(Event.COMPLETE, onFileLoaded);
			loadFile.load();
		}
		
		private function onFileLoaded(e:Event):void 
		{
			loader.loadBytes(e.target.data);
			trace(loader.scaleX);
			trace(loader.scaleY);
			loader.scaleX = 0.9;
			loader.scaleY = 0.9;
			addChild(loader);
		}
		
		private function onMouseClick(e:MouseEvent):void 
		{
			trace(e.toString());
		}
		
		private function onKeyDown(e:KeyboardEvent):void 
		{
			trace(e.toString());
			if (e.keyCode == 83) 
			{
				trace("start record");
				try 
				{
					startRecord(e);
				}
				catch (err:Error)
				{
					trace(err.getStackTrace());
				}
				
			}
			else if(e.keyCode == 84) 
			{
				trace("stop record");
				try
				{
					stopRecord(e);
				}
				catch (err:Error)
				{
					trace(err.getStackTrace());
				}
			}
		}
		
		private function startRecord( event:Event ):void
		{
			screenCapture.record();
		}

		private function stopRecord( event:Event ):void
		{
			screenCapture.stop();
			saveFile.save(screenCapture.data, "video.flv");
		}

		private function playVideo( event:Event ):void
		{
			screenCapture.play();
		}
		
	}
	
}